package com.huilian.iotbox.client.controller;

import com.huilian.iotbox.client.security.jwt.util.JwtTokenUtil;
import com.huilian.iotbox.client.service.ErrorLogService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.ErrorLogDto;
import com.huilian.iotbox.data.dto.MemberDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/api/error")
public class ErrorLogController extends BaseController {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private ErrorLogService errorLogService;

    @PostMapping("/catchAnomaly")
    public ServerResponse catchAnomaly(@RequestBody ErrorLogDto errorLogDto, HttpServletRequest request) throws Exception {
        MemberDto memberFromReq = null;
        try {
             memberFromReq = jwtTokenUtil.getMemberFromReq(request);
        } catch (Exception e){
            e.printStackTrace();
        }
        if (!StringUtils.isEmpty(memberFromReq)){
            log.info("前端异常_1：{}", memberFromReq.getId());
        }
        log.info("前端异常_2：{}", errorLogDto.getErrorMsg());
        return errorLogService.catchAnomaly(errorLogDto);
    }
}
